﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Forest.Lion.Domain.Contracts;
using Microsoft.Practices.ServiceLocation;
using Forest.Lion.Web.Models.Company;
using Forest.Lion.Domain.Services;

namespace Forest.Lion.Web.Controllers
{
    public class CompanyController : AuthorizedController
    {
        //
        // GET: /Company/

        public CompanyController(IUserServices userServices,
            IServiceLocator serviceLocator)
            : base(userServices, serviceLocator)
        {
        }
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Show(Guid id)
        {
            return View();
        }

        public ActionResult Edit()
        {
            var entity = Using<CompanyAppService>().GetById(this.CurrentUserId);


            var formModel = new CompanyFormModel
            {
                Id = entity.Id,
                CompanyName = entity.CompanyName,
                ShortName = entity.ShortName,
                Shortcut = entity.Shortcut,
                Industry = entity.Industry,
                CompanyType = entity.CompanyType,
                Employees = entity.Employees,
                BusinessLicense = entity.BusinessLicense,
                RegisteredCapital = entity.RegisteredCapital,
                EstablishedOn = entity.EstablishedOn,
                Overview = entity.Overview,
                Business = entity.Business,
                Province = entity.Province,
                City = entity.City,
                Address = entity.Address,
                PostalCode = entity.PostalCode,
                Contact = entity.Contact,
                Tel = entity.Tel,
                Fax = entity.Fax,
                Mobile = entity.Mobile,
                Email = entity.Email,
                WebSite = entity.WebSite,
                Park = entity.Park,
                EnableShortName = entity.EnableShortName,
                TemplateId = entity.TemplateId
            };

            PrepareIndustryModel(formModel);
            PrepareCompanyTypeModel(formModel);
            return View(formModel);
        }


        [HttpPost]      
        public ActionResult Edit(CompanyFormModel formModel)
        {
            if (ModelState.IsValid)
            {
                // Using<UpdateCompany>().Execute(formModel);
                Using<CompanyAppService>().Update(formModel);
            }
            PrepareIndustryModel(formModel);
            PrepareCompanyTypeModel(formModel);
            return View(formModel);

        }

        public ActionResult List() {

            return View();
        }

        [NonAction]
        protected void PrepareIndustryModel(CompanyFormModel model)
        {
            if (model == null)
                throw new ArgumentNullException("model");

            var industries = Using<LovHandler>().GetListByParentId(Guid.Parse("00000000-0000-0000-0000-000000200000"));

            foreach (var industry in industries)
            {
                model.AvailableIndustres.Add(new SelectListItem()
                {
                    Text = industry.Name,
                    Value = Convert.ToString(industry.Value)
                });
            }
        }

        [NonAction]
        protected void PrepareCompanyTypeModel(CompanyFormModel model)
        {
            if (model == null)
                throw new ArgumentNullException("model");

            var lovs = Using<LovHandler>().GetListByParentId(Guid.Parse("00000000-0000-0000-0000-000000100000"));

            foreach (var lov in lovs)
            {
                model.AvailableCompanyTypes.Add(new SelectListItem()
                {
                    Text = lov.Name,
                    Value = Convert.ToString(lov.Value)

                });
            }
        }
    }
}
